#include "unp.h"

void sig_alarm(int signo)
{
  return;
}

int main(int argc, char** argv)
{
  int sockfd,n;
  struct sockaddr_un servaddr,cliaddr;
  socklen_t len;
  char recvline[4096] = {'\0'};
  struct timeval tv_out;
  
  if (argc < 2)
  {
    printf("usage: server <socket path>\n");
    exit(-1);
  }
  
  memset(&servaddr,0,sizeof(servaddr));
  memset(&cliaddr,0,sizeof(cliaddr));
  
  sockfd = socket(AF_LOCAL,SOCK_DGRAM,0);
  unlink(argv[1]);
  servaddr.sun_family = AF_LOCAL;
  strcpy(servaddr.sun_path,argv[1]);
  bind(sockfd,(struct sockaddr*)(&servaddr),sizeof(servaddr));
  
  tv_out.tv_sec = 2;
  //setsockopt(sockfd,SOL_SOCKET,SO_RCVTIMEO,&tv_out,sizeof(tv_out));
  
  for(;;)
  {
    printf("recving msg...\n");
    len = sizeof(cliaddr);
    n = recvfrom(sockfd,recvline,sizeof(recvline),0,(struct sockaddr*)&cliaddr,&len);
    if (n > 0)
      sendto(sockfd,recvline,n,0,(const struct sockaddr*)&cliaddr,len);
  } 
 
  return 0;
}
